package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.UserInfo;
import com.cq.hd.common.enums.biz.AgentStateEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbAgentBindApplyApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Slf4j
@RestController
@RequestMapping("/agentBindApply")
@Api(tags = "经纪人绑定相关接口")
public class AgentBindApplyController {

    @Autowired
    private TbAgentBindApplyApi agentBindApplyApi;

    @ApiOperation(value = "经纪人入驻预申请（供用户点击入驻申请链接调用）")
    @ApiResponse(response = PreAgentApplyMerchantVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/preApply")
    public Result<PreAgentApplyVo> preApply(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                            @RequestBody PreAgentEnterApplyDto preAgentApplyMerchantDto) {
        preAgentApplyMerchantDto.setUserId(appLoginUser.getUserId());
        return agentBindApplyApi.preApply(preAgentApplyMerchantDto);
    }

    @ApiOperation(value = "经纪人申请绑定")
    @PostMapping("/apply")
    public Result<AgentBindApplyVo> apply(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                          @RequestBody AgentBindApplyDto agentApplyDto) {
        agentApplyDto.setUserId(appLoginUser.getUserId());
        return agentBindApplyApi.apply(agentApplyDto);
    }

    @ApiOperation(value = "我的入驻申请记录")
    @ApiResponse(response = MyAgentBindApplyPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/myApplyPage")
    public Result<Page<MyAgentBindApplyPageVo>> myApplyPage(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                                        MyAgentBindApplyPageDto myAgentBindApplyPageDto) {
        UserInfo userInfo = appLoginUser.getUserInfo();
        myAgentBindApplyPageDto.setApplyUserId(appLoginUser.getUserId());
        if (userInfo == null || userInfo.getAgentState().equals(AgentStateEnum.CLOSE.getValue())) {
            return ResultGenerator.success(new Page<>());
        }
        myAgentBindApplyPageDto.setAgentLevel(userInfo.getAgentLevel());
        return agentBindApplyApi.myApplyPage(myAgentBindApplyPageDto);
    }

    @ApiOperation(value = "入驻申请列表")
    @ApiResponse(response = AgentBindApplyPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/page")
    public Result<Page<AgentBindApplyPageVo>> page(@ApiIgnore @LoginUser AppLoginUser appLoginUser, AgentBindApplyPageDto agentBindApplyPageDto) {
        agentBindApplyPageDto.setInviterUserId(appLoginUser.getUserId());
        return agentBindApplyApi.page(agentBindApplyPageDto);
    }

    @ApiOperation(value = "审核经纪人入驻申请")
    @PostMapping("/audit")
    public Result<Boolean> audit(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                 @RequestBody AgentBindApplyAuditDto auditDto) {
        auditDto.setInviterUserId(appLoginUser.getUserId());
        return agentBindApplyApi.audit(auditDto);
    }

    @ApiOperation(value = "待审核申请数量")
    @GetMapping("/waitAuditCount")
    public Result<Integer> waitAuditCount(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        if (appLoginUser == null) {
            return ResultGenerator.success(0);
        }

        return agentBindApplyApi.waitAuditCount(appLoginUser.getUserId());
    }

}
